<template>
    <div class="page-100 login-register-part">
        <yd-cell-item style="padding-bottom: 20px">
            <img  slot="left" class="login-img" src="../../assets/loginxingming.png">
            <input slot="right" type="text" placeholder="姓名" v-model="name" maxlength="20">
        </yd-cell-item>
        <yd-cell-item style="padding-bottom: 20px">
            <img  slot="left" class="login-img" src="../../assets/loginsouzaidi.png">
            <input @click="handleAddressClick"  readonly="value" slot="right" type="text" placeholder="所在地" v-model="address">
        </yd-cell-item>
        <yd-cell-item style="padding-bottom: 20px">
            <img  slot="left" class="login-img" src="../../assets/logindanwei.png">
            <input @click="handleShowPlace"  readonly="value" slot="right" type="text" placeholder="所在单位" v-model="bankName">
        </yd-cell-item>
        <yd-cell-item style="padding-bottom: 20px">
            <img  slot="left" class="login-img" src="../../assets/loginjigou.png">
            <input @click="handleInstClick"  readonly="value" slot="right" type="text" placeholder="所属机构" v-model="instName">
        </yd-cell-item>
        <weui-distpicker
                v-if="isShowAddresPick"
                @confirm="onAddresConfirm"
                @cancel="onAddresCancel"
                province="浙江省" city="杭州市" area="滨江区"></weui-distpicker>
        <yd-button size="large" type="primary" @click.native="handleClick" style="background-color:#3cc8a8;color:white;height: 40px">提交</yd-button>

    </div>
</template>
<script>
    import {getAllPartnerInfos, getInstInfos, registry2, updateRegistInfo} from "../../pcof/post";
    import {openId, registryFlag, userInfoData} from '../../globalVariables'
    import WeuiDistpicker from 'weui-distpicker';

    export default {
        name: "loginRegistry",
        component: {
            WeuiDistpicker,
        },
        data() {
            // var obj = JSON.stringify(city);

            return {
                isregistry: '0',//0是注册页 3是修改资料页
                name: '',
                bankUuid: '',//单位
                bankName: '',
                province: '',
                city: '',
                district: '',
                address: '',
                instuuid: '',//所属机构
                instName: '',//机构
                isShowAddresPick: false,
            }
        },
        created() {

            this.isregistry = this.$route.params.isregistry;
            console.log('数据' + this.isregistry);

            if (this.isregistry === '0') {

            } else if (this.isregistry === '3') {
                this.name = JSON.parse(localStorage.getItem(userInfoData)).name;
                this.bankName = JSON.parse(localStorage.getItem(userInfoData)).partnerName;
                this.bankUuid = JSON.parse(localStorage.getItem(userInfoData)).partnerUuid;
                this.province = JSON.parse(localStorage.getItem(userInfoData)).province;
                this.city = JSON.parse(localStorage.getItem(userInfoData)).city;
                this.district = JSON.parse(localStorage.getItem(userInfoData)).district;
                this.address = JSON.parse(localStorage.getItem(userInfoData)).province + JSON.parse(localStorage.getItem(userInfoData)).city + JSON.parse(localStorage.getItem(userInfoData)).district;
                this.instName = JSON.parse(localStorage.getItem(userInfoData)).bankName;
                this.instuuid = JSON.parse(localStorage.getItem(userInfoData)).bankUuid;

            }

        },
        mounted() {
            let that = this;

            var geolocation = new BMap.Geolocation();
            // 创建地理编码实例
            var myGeo = new BMap.Geocoder();
            geolocation.getCurrentPosition(function (r) {
                if (this.getStatus() == BMAP_STATUS_SUCCESS) {
                    var pt = r.point;
                    // 根据坐标得到地址描述
                    myGeo.getLocation(pt, function (result) {
                        if (result) {
                            var addComp = result.addressComponents;
                            that.province = addComp.province;
                            that.city = addComp.city;
                            that.district = addComp.district;
                            console.log(addComp);
                            console.log(that.district + "," + that.district + '，' + that.district);
                            that.address = that.province + that.city + that.district;
                        }
                    });
                }
            });
        },
        methods: {
            /**异步请求函数 加上async 注册请求 */
            async handleClick() {
                if (this.name === '' || this.name.length < 2 || this.name.length > 5) {
                    this.$dialog.toast({
                        mes: "请输入真实姓名!",
                        timeout: 1500
                    });
                    return;
                }
                if (this.address === '') {
                    this.$dialog.toast({
                        mes: "请先选择所在地!",
                        timeout: 1500
                    });
                    return;

                }
                if (this.bankUuid === '') {
                    this.$dialog.toast({
                        mes: "请先选择所在单位!",
                        timeout: 1500
                    });
                    return;

                }
                if (this.instuuid === '') {
                    this.$dialog.toast({
                        mes: "请选择所属机构!",
                        timeout: 1500
                    });
                    return;

                }
                //0是注册页 3是修改资料页
                if (this.isregistry === '0') {
                    //    name,province,city,district,inst_uuid,openid,registryFlag

                    var response = await registry2(this.name, this.province, this.city, this.district, this.instuuid, localStorage.getItem(openId), localStorage.getItem(registryFlag));
                    if (response.data.code === 200) {
                        //用户状态：0-正常，1-审批中，3-被拒绝 登录之后
                        console.log(response);
                        console.log(response.data.data.state);
                        var state = response.data.data.state;
                        if (state === '0') {
                            this.$router.push({name: 'homeDetail', params: {mobile: JSON.parse(localStorage.getItem(userInfoData)).mobile}});
                        } else if (state === '1' || state === '3') {
                            this.$router.push({name: 'loginCheckinfo', params: {loginUserState: state}});
                        }
                    } else {
                        this.$dialog.toast({
                            mes: response.data.msg,
                            timeout: 1500
                        });
                    }
                } else if (this.isregistry === '3') {
                    // customerType,uuid,mobile,name,bankUuid,province,city,district

                    var response = await updateRegistInfo('01',JSON.parse(localStorage.getItem(userInfoData)).uuid,
                        JSON.parse(localStorage.getItem(userInfoData)).mobile,
                        this.name, this.instuuid, this.province, this.city, this.district);
                    if (response.data.code === 200) {
                        this.$router.push({name: 'loginCheckinfo', params: {loginUserState: '1'}});

                    } else {
                        this.$dialog.toast({
                            mes: response.data.msg,
                            timeout: 1500
                        });
                    }


                }


            },

            /** 异步请求函数 加上async */
            handleAddressClick() {
                this.isShowAddresPick = true;
            },
            /** 确定所在地Picker回调监听 */
            onAddresConfirm(data) {
                this.isShowAddresPick = false;
                this.province = data[0].label;
                this.city = data.length >= 2 ? data[1].label : '';
                this.district = data.length == 3 ? data[2].label : '';
                this.address = this.province + this.city + this.district;

            },
            /** 取消所在地Picker回调监听 */
            onAddresCancel() {
                this.isShowAddresPick = false;
            },
            /** 显示所在单位事件 */
            async handleShowPlace() {
                var response = await getAllPartnerInfos();
                if (response.data.code === 200) {
                    let that = this;
                    var arr = response.data.data;
                    if (arr.length > 0) {
                        var newAr = new Array();
                        for (var i = 0; i < arr.length; i++) {
                            var item = response.data.data[i];
                            newAr.push({'label': item.name, 'value': item.uuid});
                        }
                        weui.picker(
                            newAr,
                            {
                                onChange(result) {
                                    console.log(result);

                                },
                                onConfirm(result) {
                                    that.bankName = result[0].label;
                                    that.bankUuid = result[0].value;
                                    that.instuuid='';
                                    that.instName='';
                                },
                            },
                        );
                    } else {
                       this.bankUuid='';
                       this.bankName='';
                        this.$dialog.toast({
                            mes: "当前没有单位!",
                            timeout: 1500
                        });
                        return;


                    }

                }
            },
            //选择所属机构
            //000000000011 杭州银行总行
            //目前只有杭州银行总行 有数据
            async handleInstClick() {
                if (this.address === '') {
                    this.$dialog.toast({
                        mes: "请先选择所在地!",
                        timeout: 1500
                    });
                    return;
                }
                if (this.place === '') {
                    this.$dialog.toast({
                        mes: "请先选择所在单位!",
                        timeout: 1500
                    });
                    return;

                }

                var response = await getInstInfos(this.bankUuid, this.province, this.city, this.district);
                if (response.data.code === 200) {
                    let that = this;
                    var arr = response.data.data;
                    if (arr.length > 0) {
                        var newAr = new Array();
                        for (var i = 0; i < arr.length; i++) {
                            var item = response.data.data[i];
                            newAr.push({'label': item.name, 'value': item.uuid});
                        }
                        weui.picker(
                            newAr,
                            {
                                onChange(result) {

                                },
                                onConfirm(result) {

                                    that.instName = result[0].label;
                                    that.instuuid = result[0].value;

                                },
                            },
                        );

                    } else {
                        this.instuuid='';
                        this.instName='';
                        this.$dialog.toast({
                            mes: '当前没有机构!',
                            timeout: 1500
                        });
                        return;
                    }

                } else {
                    this.$dialog.toast({
                        mes: response.data.msg,
                        timeout: 1500
                    });
                }
            },

        }


    }
</script>

<style lang="scss">
    @import "../login/login.scss";
</style>